<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class ChangeUsernameRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_name'=>'required|string|max:20'

        ];
    }

    public function messages()
    {
        return [
            'user_name.required'=>'姓名必填',
            'user_name.string'  =>'请输入正确格式',
            'user_name.max'     =>'不能超过20个字符'
        ];
    }


    protected function failedValidation(Validator $validator)
    {
        throw(new HttpResponseException(response()->json(['massages' => $validator->errors()->all(), 'code' => '100'])));
    }


}
